home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////
- // //
- // Generator.c //
- // //
- // Copyright 1996 1 A.M. Productions //
- // Aug 8,1996 //
- // //
- // This demonstrates serial encoding based //
- // on simple mathmatical functions performed //
- // on one seed number split in half. Both //
- // encoding processes are the same for the //
- // sake of demonstration. //
- // //
- // Output is saved as a tab delimited Excel //
- // document. //
- //////////////////////////////////////////////////
-
- #ifndef __MAIN__
- #include "Generator.h"
- #endif
-
- void main()
- {
- long start, end;
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent,0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
-
- GetNumbers(&start,&end); // Go ask the user for the range of numbers
- CalculateValues(start,end); // Save them out to a file
-
- FlushEvents(everyEvent,0);
- }
-
- void CalculateValues(long start, long end)
- {
- long lp, inOutCount;
- short fRefNum;
- StandardFileReply fReply;
- Ptr bufPtr;
- Str255 converted;
- Str255 header = "\pCustomer #\tVersion\tSerial Number\n";
- Str255 cusNum = "\p000000 ";
- OSErr iErr;
-
-
- StandardPutFile("\pSave Serial Numbers","\pSerial Numbers",&fReply); // Get a filename from user
-
- if (fReply.sfReplacing) // If replacing old file then delete
- iErr = FSpDelete(&fReply.sfFile);
-
- if (fReply.sfGood)
- {
- FSpCreate(&(fReply).sfFile,'XCEL','TEXT',fReply.sfScript); // Make the file
- iErr = FSpOpenDF(&(fReply).sfFile,fsRdWrPerm,&fRefNum); // Open this file
- if (iErr) // Any problems quit
- ExitToShell();
- }
-
- inOutCount = header[0]; // Set amount to length of string
- bufPtr = (char *)&header[1]; // Point to the first character in string
- iErr = FSWrite(fRefNum,&inOutCount,bufPtr); // Write header string to file
-
- for (lp=start; lp<=end; lp++)
- {
- LongToStr(lp,cusNum); // Convert number to string
- inOutCount = 9; // Number of digits in customer number + tab + space + tab
- cusNum[7] = 0x09; // Force a tab into the string
- cusNum[9] = 0x09; // Force another tab into the string
- bufPtr = (char *)&cusNum[1]; // Point to the first character in string
- iErr = FSWrite(fRefNum,&inOutCount,bufPtr); // Write customer number, tab, version, tab
- if (iErr) // Any problems - exit
- ExitToShell();
- ProduceValue(lp, converted); // Convert number to code
- inOutCount = 16; // Number of digits in serial number plus three dashes + return
- converted[16]=0x0d;
- bufPtr = (char *)&converted[1]; // Point to the first character in string
- iErr = FSWrite(fRefNum,&inOutCount,bufPtr); // Write serial number
- if (iErr)
- {
- iErr = FSClose(fRefNum);
- ExitToShell();
- }
- if (ThisKey(kESC)) // Just in case this is taking way
- { // too long! Abort key.
- iErr = FSClose(fRefNum);
- ExitToShell();
- }
- }
-
- iErr = FSClose(fRefNum); // Close up the file
- if (iErr) // Any problems - quit
- ExitToShell();
-
- }
-
- void ProduceValue(long num, Str255 retStr)
- {
- long i,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12;
- long val1,val2,val3,val4;
- long temp1, temp2;
- Str255 valStr;
-
- i=1;
-
- LongToStr(num,valStr);
-
- d1 = valStr[i++] - '0';
- d2 = valStr[i++] - '0';
- d3 = valStr[i++] - '0';
- d10 = valStr[i++] - '0';
- d11 = valStr[i++] - '0';
- d12 = valStr[i++] - '0';
-
- d1 = (d1*100);
- d2 = (d2*10);
-
- temp1 = d1+d2+d3;
-
- d2 = d2 + 10;
- d3 = d3 + 3;
- d2 = d2 + ((d3/10)*10);
- d1 = d1 + ((d2/100)*100);
- d1 = d1-((d1/1000)*1000);
- d2 = d2-((d2/100)*100);
- d3 = d3-((d3/10)*10); // Add 13
-
- val1 = (d3*100)+(d2)+(d1/100); // Stir Numbers
- val1 = val1 * 3; // Multiply by 3
- val1 = val1 - (temp1*2); // Subtract the original * 2
- if (val1<0)
- val1 = -val1; // Adjust if necessary
- d3 = (val1 % 10); // Reassign values
- d2 = ((val1 % 100)-d3)/10;
- d1 = ((val1 % 1000)-((d2*10)+d3))/100;
-
- val1 = (d1*100)+(d2*10)+d3;
-
- // Second Number Set
-
- d10 = (d10*100);
- d11 = (d11*10);
-
- temp2 = d10+d11+d12;
-
- d11 = d11 + 10;
- d12 = d12 + 3;
- d11 = d11 + ((d12/10)*10);
- d10 = d10 + ((d11/100)*100);
- d10 = d10-((d10/1000)*1000);
- d11 = d11-((d11/100)*100);
- d12 = d12-((d12/10)*10); // Add 13
-
- val4 = (d12*100)+(d11)+(d10/100); // Stir Numbers
- val4 = val4 * 3; // Multiply by 3
- val4 = val4 - (temp2*2); // Subtract the original * 2
- if (val4<0)
- val4 = -val4; // Adjust if necessary
- d12 = (val4 % 10); // Reassign values
- d11 = ((val4 % 100)-d12)/10;
- d10 = ((val4 % 1000)-((d11*10)+d12))/100;
-
- val4 = (d10*100)+(d11*10)+d12;
-
- retStr[0] = 15; // Set string length to digits + 3 dashes
- retStr[1] = valStr[1];
- retStr[2] = valStr[2];
- retStr[3] = valStr[3];
- retStr[4] = '-';
- if (val1<1000)
- retStr[5] = (((val1 % 1000)-((d2*10)+d3))/100)+'0';
- else
- retStr[5] = '9';
- retStr[6] = (((val1 % 100)-d3)/10)+'0';
- retStr[7] = (val1 % 10)+'0';
- retStr[8] = '-';
- if (val4<1000)
- retStr[9] = (((val4 % 1000)-((d11*10)+d12))/100)+'0';
- else
- retStr[9] = '9';
- retStr[10] = ((val4 % 100)-d12)/10+'0';
- retStr[11] = (val4 % 10)+'0';
- retStr[12] = '-';
- retStr[13] = valStr[4];
- retStr[14] = valStr[5];
- retStr[15] = valStr[6];
-
- }
-
- void GetNumbers(long *start,long *end)
- {
- short itemHit, itemType;
- Handle itemHandle;
- Rect itemRect;
- Str255 number;
- DialogPtr dialog;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- dialog=GetNewDialog(128,nil,(WindowPtr)-1);
- ShowWindow(dialog);
- SelectWindow(dialog);
- SetPort(dialog);
- GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
- SetDialogItemText(itemHandle,"\p1");
- GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
- SetDialogItemText(itemHandle,"\p100");
-
- itemHit=-1;
-
- while ((itemHit !=1) && (itemHit !=2))
- ModalDialog(StdFilter, &itemHit);
-
- if (itemHit == 2)
- {
- SetPort(oldPort);
- DisposeDialog(dialog);
- ExitToShell();
- }
- if (itemHit == 1)
- {
- GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
- GetDialogItemText(itemHandle,number);
- StringToNum(number,start);
-
- GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
- GetDialogItemText(itemHandle,number);
- StringToNum(number,end);
-
- if ((*end)<(*start))
- {
- SetPort(oldPort);
- *start = *end;
- *end = (*end) + 100;
- }
- if ((*end) > 999999)
- (*end) = (*start) + 10;
- }
- SetPort(oldPort);
- DisposeDialog(dialog);
- }
-
- pascal Boolean StdFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
- {
- char theChar;
- short itemKind;
- Handle itemHandle;
- Rect itemBox;
-
- switch ( theEvent->what )
- {
- case keyDown:
- theChar = (char)(theEvent->message & charCodeMask);
- if ( (((theEvent->modifiers & cmdKey) != 0) && (theChar == '.')) || (theChar == (char)27) ) /*cmd-. or ESC*/
- {
- *itemHit = kCancelButton;
- GetDItem(theDialog, kCancelButton, &itemKind, &itemHandle, &itemBox);
- HiliteControl((ControlHandle)itemHandle, 1);
- return true;
- }
- if ( (theChar == (char)13) || (theChar == (char)3) )
- {
- *itemHit = kOKButton;
- GetDItem(theDialog, 1, &itemKind, &itemHandle, &itemBox);
- HiliteControl((ControlHandle)itemHandle, kOKButton);
- return true;
- }
- break;
- case updateEvt:
- BeginUpdate(theDialog);
- SetPort(theDialog);
- DrawDialog(theDialog);
- GetDItem(theDialog, kOKButton, &itemKind, &itemHandle, &itemBox);
- InsetRect(&itemBox, -4, -4);
- PenSize(3, 3);
- FrameRoundRect(&itemBox, 15, 15);
- EndUpdate(theDialog);
- break;
- }
- return false;
- }
-
- void LongToStr(long num, Str255 str)
- {
- long d, counter=6;
-
- if (num)
- {
- d = num;
- for (d=1; d<7; d++)
- str[d] = '0';
- str[counter+1] = '\0';
- str[0] = counter;
- while (num > 0)
- {
- d = num/10;
- str[counter--] = num - (d*10) + '0';
- num = d;
- }
- } else
- {
- str[0] = 1;
- str[1] = '0';
- str[2] = '\0';
- }
- }